
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAXLINE 50
int j;
pid_t pid1,pid2;
void Stoper(int sign0){
    kill(pid1,SIGUSR1);
    waitpid(pid1,NULL,0);
    kill(pid2,SIGUSR1);
    waitpid(pid2,NULL,0);
    printf("Parent process is killed!");
    exit(0);
};
void Stop(int sign0){
    printf(" child process  %d killed by parent!\n",getpid());
    printf("子进程收到的消息数量:%d\n",j);
    exit(0);
};
int main(void)
{
    int n;
    int fd[2];
    pipe(fd);
    int i=0;
    j=0;
    pid1 = fork();
    if (pid1>0){pid2 = fork();}else pid2=0;
    //if(pid1)==0)            /* 父进程把文件描述符复制给子进程 */
    //    exit(1);
while(1)
{    if(pid1 > 0&&pid2>0){                /* 父进程写 */
        close(fd[0]);                /* 关闭读描述符 */
        char atcmd10[MAXLINE];
        sprintf(atcmd10,"I send you %d times! \n",++i);
        write(fd[1],atcmd10 , 30);
        sleep(1);
        signal(SIGINT, Stoper); 
        if (i==5){Stoper(0);}
    }
    else  {         
             
        signal(SIGINT,SIG_IGN);
        signal(SIGUSR1,Stop);
        close(fd[1]);                
        sleep(1);
        char line[256];
        n = read(fd[0], line,30);
        printf("%d的消息：%s\n",getpid(),line);   
        j++;   
    }
 }}


